package admin

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	apiAdmin "miaoyu/api/backend/admin"
	modelAdmin "miaoyu/internal/model/admin"

	"miaoyu/internal/enums/menuenum"
	"miaoyu/internal/service"
)

var Menu = new(menuCtl)

type menuCtl struct {
}

func (c *menuCtl) Index(ctx context.Context, req *apiAdmin.MenuIndexReq) (res *apiAdmin.MenuIndexRes, err error) {
	service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "backend/public/layout.html").WriteWithExit(g.MapStrAny{
		"mainTpl": "backend/menu/index.html",
		"footTpl": "backend/menu/index.js.html",
		"title":   "菜单管理",
	})
	return
}

func (c *menuCtl) DoList(ctx context.Context, req *apiAdmin.MenuListDoReq) (res *apiAdmin.MenuListDoRes, err error) {
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	menuRes, errM := service.Menu().GetAllList(ctx, g.MapStrAny{})
	if errM != nil {
		err = errM
		return
	}
	res = &apiAdmin.MenuListDoRes{}
	if err = gconv.Scan(menuRes, &res.MenuList); err != nil {
		return
	}
	commonJson.SetSuccess("ok", res.MenuList).WriteJsonExit()

	return
}
func (c *menuCtl) MenuAdd(ctx context.Context, req *apiAdmin.MenuAddReq) (res *apiAdmin.MenuAddRes, err error) {
	var parentId = req.ParentId

	menuList, errM := service.Menu().GetMenuList(ctx, g.MapStrAny{})
	if errM != nil {
		err = errM
		return
	}
	service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "backend/public/layout.html").WriteWithExit(g.MapStrAny{
		"mainTpl":        "backend/menu/add.html",
		"footTpl":        "backend/menu/add.js.html",
		"siteName":       "控制台",
		"menuList":       menuList,
		"menuTypeList":   menuenum.ListTypeMap,
		"menuStatusList": menuenum.ListStatusMap,
		"parentId":       parentId,
	})

	return
}

func (c *menuCtl) DoMenuSubmit(ctx context.Context, req *apiAdmin.MenuSubmitDoReq) (res *apiAdmin.MenuSubmitDoRes, err error) {

	var menuInput modelAdmin.MenuInput
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	err = gconv.Scan(req, &menuInput)
	if err != nil {
		return
	}
	menuId := req.MenuId
	if menuId != 0 {
		_, err = service.Menu().Edit(ctx, menuInput, menuId)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		} else {
			commonJson.SetSuccessJsonExit("修改成功", nil)
		}
		return
	} else {
		_, err = service.Menu().Add(ctx, menuInput)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		}

		commonJson.SetSuccessJsonExit("添加成功", nil)
	}

	//_, err = service.Menu().Add(ctx, menuAdd)
	//if err != nil {
	//	commonJson.SetErrorJson(err.Error())
	//	return
	//}
	//
	//commonJson.SetSuccessJson("添加成功", nil)
	return
}
func (c *menuCtl) MenuEdit(ctx context.Context, req *apiAdmin.MenuEditReq) (res *apiAdmin.MenuEditRes, err error) {
	menuId := req.MenuId

	menuRes, resErr := service.Menu().GetMenuById(ctx, menuId)
	if resErr != nil {
		err = resErr
		service.UniResp().UniResp(g.RequestFromCtx(ctx), err.Error(), "")
		return
	}
	menuList, errM := service.Menu().GetMenuList(ctx, g.MapStrAny{})
	if errM != nil {
		err = errM
		return
	}

	service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "backend/public/layout.html").WriteWithExit(g.MapStrAny{
		"mainTpl":        "backend/menu/add.html",
		"footTpl":        "backend/menu/edit.js.html",
		"siteName":       "控制台",
		"data":           menuRes,
		"menuList":       menuList,
		"menuTypeList":   menuenum.ListTypeMap,
		"menuStatusList": menuenum.ListStatusMap,
	})
	return
}
func (c *menuCtl) MenuDel(ctx context.Context, req *apiAdmin.MenuDelDoReq) (res *apiAdmin.MenuDelDoRes, err error) {
	menuId := req.MenuId
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	_, resErr := service.Menu().Del(ctx, menuId)
	if resErr != nil {
		err = resErr
		commonJson.SetErrorJsonExit(err.Error())
		return
	}
	commonJson.SetSuccessJsonExit("操作成功", nil)
	return
}
